package com.jqk.cloudzuul.filter;

import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author:JiangQiKun
 * @Date:2022/03/13 03:57 PM
 * Description:
 * com.jqk.cloudzuul.filterLimitFilter
 * 网关限流：令牌桶算法
 */
@Component
public class LimitFilter2 extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return -9;
    }

    @Override
    public boolean shouldFilter() {
        Object limit = RequestContext.getCurrentContext().get("limit");
        return Objects.isNull(limit)? true:(Boolean)limit;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("限流之后的过滤器");
        return null;
    }
}
